<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>迭代器与生成器方法</title>
</head>
<body>
  <script>
    class Person {
      // 在原型上定义生成器方法
      *createNicknameIterator() {
        yield 'Jack';
        yield 'Jake';
        yield 'J-Dog';
      }

      // 在类上定义生成器方法
      static *createJobIterator() {
        yield 'Butcher';
        yield 'Baker';
        yield 'Candlestick maker';
      }
    }

    let jobIter = Person.createJobIterator();
    console.log(jobIter.next().value);  // Butcher
    console.log(jobIter.next().value);  // Baker
    console.log(jobIter.next().value);  // Candlestick maker

    let p = new Person();
    let nicknameIter = p.createNicknameIterator();
    console.log(nicknameIter.next().value);  // Jack
    console.log(nicknameIter.next().value);  // Jake
    console.log(nicknameIter.next().value);  // J-Dog
  </script>
</body>
</html>